package com.example.demosecurityjwt.service.impl;

import com.example.demosecurityjwt.exceptions.MyException;
import com.example.demosecurityjwt.mapper.auto.TbUserMapper;
import com.example.demosecurityjwt.pojo.entity.TbUser;
import com.example.demosecurityjwt.pojo.enums.ResultEnum;
import com.example.demosecurityjwt.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: bysun
 * @Date: 2019/3/28 15:25
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private TbUserMapper tbUserMapper;

    @Override
    public TbUser getUserByName(String userName) {
        if (StringUtils.isEmpty(userName)) {
            throw new MyException(ResultEnum.DEFAULT_ERROR);
        }
        TbUser tbUser = new TbUser();
        tbUser.setName(userName);
        return getUserByTbUser(tbUser);
    }

    public TbUser getUserByTbUser(TbUser tbUser) {
        List<TbUser> users = tbUserMapper.select(tbUser);
        // 无用户
        if (users == null || users.size() <= 0) {
            throw new MyException(ResultEnum.DEFAULT_ERROR);
        // 用户有问题
        } else if (users.size() > 1) {
            throw new MyException(ResultEnum.DEFAULT_ERROR);
        }
        return users.get(0);
    }
}
